﻿using System;

namespace RPC {
	/// <summary>
	/// A Unique id for the object.
	/// </summary>
	[AttributeUsage(AttributeTargets.Class,AllowMultiple=false,Inherited=false)]
	public sealed class RPCID : Attribute {
		#region Fields
		private readonly UInt16 _id;
		public static readonly RPCID Empty = new RPCID(0);
		#endregion
		#region Constructor
		/// <summary>
		/// Initializes a new instance of the <see cref="RPCID"/> class.
		/// </summary>
		/// <param name="messageID">The unique message ID.</param>
		public RPCID(UInt16 messageID) {
			_id = messageID;
		}
		#endregion
		#region Public
		/// <summary>
		/// Returns a <see cref="System.String"/> that represents this instance.
		/// </summary>
		/// <returns>
		/// A <see cref="System.String"/> that represents this instance.
		/// </returns>
		public override string ToString() {
			return this.ID.ToString();
		}
		#endregion
		#region Static
		/// <summary>
		/// Parses the ID.
		/// </summary>
		/// <param name="obj">The obj.</param>
		/// <returns></returns>
		public static UInt16 ParseID(Object obj) {
			RPCID id = ParseAttribute(obj);
			return (id != null ? id.ID : (UInt16)0);
		}
		/// <summary>
		/// Parses the ID.
		/// </summary>
		/// <param name="type">The type.</param>
		/// <returns></returns>
		public static UInt16 ParseID(Type type) {
			RPCID id = ParseAttribute(type);
			return (id != null ? id.ID : (UInt16)0);
		}
		/// <summary>
		/// Parses the has ID.
		/// </summary>
		/// <param name="obj">The obj.</param>
		/// <returns></returns>
		public static Boolean ParseHasID(Object obj) {
			return (ParseID(obj) > Empty.ID);
		}
		/// <summary>
		/// Parses the has ID.
		/// </summary>
		/// <param name="type">The type.</param>
		/// <returns></returns>
		public static Boolean ParseHasID(Type type) {
			return (ParseID(type) > Empty.ID);
		}
		/// <summary>
		/// Parses the attribute.
		/// </summary>
		/// <param name="obj">The obj.</param>
		/// <returns></returns>
		public static RPCID ParseAttribute(Object obj) {
			if (obj != null) {
				return ParseAttribute(obj.GetType());
			}
			return null;
		}
		/// <summary>
		/// Parses the attribute.
		/// </summary>
		/// <param name="type">The type.</param>
		/// <returns></returns>
		public static RPCID ParseAttribute(Type type) {
			if (type != null) {
				Object[] att = type.GetCustomAttributes(typeof(RPCID), false);
				if (att != null && att.Length > 0) {
					return ((RPCID)att[0]);
				}
			}
			return null;
		}

		#endregion
		#region Property
		/// <summary>
		/// Gets the unique ID.
		/// </summary>
		/// <value>The ID.</value>
		public UInt16 ID {
			get {
				return _id;
			}
		}
		/// <summary>
		/// Gets a value indicating whether this instance has unique ID.
		/// </summary>
		/// <value><c>true</c> if this instance has unique ID; otherwise, <c>false</c>.</value>
		public Boolean HasID {
			get {
				return (this.ID > Empty.ID);
			}
		}
		#endregion
	}
}
